package com.xxl.sso.core.login;

import javax.servlet.http.HttpServletRequest;

import com.xxl.sso.core.conf.Conf;
import com.xxl.sso.core.store.SsoLoginStore;
import com.xxl.sso.core.store.SsoSessionIdHelper;
import com.xxl.sso.core.user.XxlSsoUser;

/**
 * @author xuxueli 2018-11-15 15:54:40
 */
public class SsoTokenLoginHelper {
	/**
	 * client login
	 *
	 * @param sessionId
	 * @param xxlUser
	 */
	public static void login(String sessionId, XxlSsoUser xxlUser) {
		String storeKey = SsoSessionIdHelper.parseStoreKey(sessionId);
		if (storeKey == null) {
			throw new RuntimeException("parseStoreKey Fail, sessionId:" + sessionId);
		}
		SsoLoginStore.put(storeKey, xxlUser);
	}

	/**
	 * client logout
	 *
	 * @param sessionId
	 */
	public static void logout(String sessionId) {
		String storeKey = SsoSessionIdHelper.parseStoreKey(sessionId);
		if (storeKey == null) {
			return;
		}
		SsoLoginStore.remove(storeKey);
	}

	/**
	 * client logout
	 *
	 * @param request
	 */
	public static void logout(HttpServletRequest request) {
		String headerSessionId = request.getHeader(Conf.SSO_SESSIONID);
		logout(headerSessionId);
	}

	/**
	 * login check
	 *
	 * @param sessionId
	 * @return
	 */
	public static XxlSsoUser loginCheck(String sessionId) {
		String storeKey = SsoSessionIdHelper.parseStoreKey(sessionId);
		if (storeKey == null) {
			return null;
		}
		XxlSsoUser xxlUser = SsoLoginStore.get(storeKey);
		if (xxlUser != null) {
			String version = SsoSessionIdHelper.parseVersion(sessionId);
			if (xxlUser.getVersion().equals(version)) {
				// After the expiration time has passed half, Auto refresh
				if ((System.currentTimeMillis() - xxlUser.getExpireFreshTime()) > xxlUser.getExpireMinite() / 2) {
					xxlUser.setExpireFreshTime(System.currentTimeMillis());
					SsoLoginStore.put(storeKey, xxlUser);
				}
				return xxlUser;
			}
		}
		return null;
	}

	/**
	 * login check
	 *
	 * @param request
	 * @return
	 */
	public static XxlSsoUser loginCheck(HttpServletRequest request) {
		String headerSessionId = request.getHeader(Conf.SSO_SESSIONID);
		return loginCheck(headerSessionId);
	}
}
